<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:pn="http://stager.uninett.no/perfsonarnc/1.0"
	xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
	xmlns:netutil="http://ggf.org/ns/nmwg/characteristic/utilization/2.0/"
	xmlns:nmwg="http://ggf.org/ns/nmwg/base/2.0/" xmlns:nmwgt="http://ggf.org/ns/nmwg/topology/2.0/">
	<xsl:output method="xml" indent="yes" />

	<xsl:template match="/">
		<soapenv:Envelope>
			<soapenv:Body>
				<nmwg:message type="SetupDataResponse">
					<xsl:for-each select="//nc:data/pn:obsPoint">
						<nmwg:metadata id="meta{pn:id}">
							<netutil:subject id="subj{pn:id}">
								<nmwgt:interface>
									<nmwgt:hostName>
										<xsl:value-of select="pn:hostname" />
									</nmwgt:hostName>
									<nmwgt:ifName>
										<xsl:value-of select="pn:name" />
									</nmwgt:ifName>
									<nmwgt:direction>
										<xsl:value-of select="pn:direction" />
									</nmwgt:direction>
									<nmwgt:capacity>
										<xsl:value-of select="pn:capacity" />
									</nmwgt:capacity>
                                    <nmwgt:ifAddress>
                                        <xsl:value-of select="pn:ipaddress"/>
                                    </nmwgt:ifAddress>                                									
									<nmwgt:ifDescription>
										<xsl:if test="pn:direction='in'">ifHCInOctets</xsl:if>
										<xsl:if test="pn:direction='out'">ifHCOutOctets</xsl:if>
									</nmwgt:ifDescription>
								</nmwgt:interface>
							</netutil:subject>
							<nmwg:eventType>http://ggf.org/ns/nmwg/characteristic/utilization/2.0</nmwg:eventType>
							<nmwg:eventType>http://ggf.org/ns/nmwg/tools/snmp/2.0</nmwg:eventType>
						</nmwg:metadata>
						<xsl:call-template name="data">
							<xsl:with-param name="id" select="pn:id" />
						</xsl:call-template>
					</xsl:for-each>
				</nmwg:message>
			</soapenv:Body>
		</soapenv:Envelope>
	</xsl:template>

	<xsl:template name="data">
		<xsl:param name="id" />
		<nmwg:data id="data{pn:id}" metadataIdRef="meta{pn:id}">		
		<xsl:for-each select="//pn:timePeriod/pn:obsPoint[pn:id=$id]">
				<netutil:datum
					xmlns:netutil="http://ggf.org/ns/nmwg/characteristic/utilization/2.0/"
					timeValue="{../pn:timestamp}" value="{pn:rows/pn:row/pn:datum[1]}"
					valueUnits="Bps" timeType="unix" />
		</xsl:for-each>
	</nmwg:data>		
	</xsl:template>

</xsl:stylesheet>
